package com.ruoyi.web.controller.blogs;

import com.ruoyi.blogs.domain.BsNote;
import com.ruoyi.blogs.service.IBsNoteService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 笔记管理Controller
 *
 * @author ruoyi
 * @date 2022-11-01
 */
@RestController
@RequestMapping("/blogs/note")
public class BsNoteController extends BaseController {
    @Autowired
    private IBsNoteService bsNoteService;

    /**
     * 查询笔记管理列表
     */
    @PreAuthorize("@ss.hasPermi('blogs:note:list')")
    @GetMapping("/list")
    public TableDataInfo list(BsNote bsNote) {
        startPage();
        List<BsNote> list = bsNoteService.selectBsNoteList(bsNote);
        return getDataTable(list);
    }

    /**
     * 导出笔记管理列表
     */
    @PreAuthorize("@ss.hasPermi('blogs:note:export')")
    @Log(title = "笔记管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BsNote bsNote) {
        List<BsNote> list = bsNoteService.selectBsNoteList(bsNote);
        ExcelUtil<BsNote> util = new ExcelUtil<BsNote>(BsNote.class);
        util.exportExcel(response, list, "笔记管理数据");
    }

    /**
     * 获取笔记管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('blogs:note:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(bsNoteService.selectBsNoteById(id));
    }

    /**
     * 新增笔记管理
     */
    @PreAuthorize("@ss.hasPermi('blogs:note:add')")
    @Log(title = "笔记管理", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping
    public AjaxResult add(@RequestBody BsNote bsNote) {
        bsNote.setId(IdUtils.fastSimpleUUID());
        return toAjax(bsNoteService.insertBsNote(bsNote));
    }

    /**
     * 修改笔记管理
     */
    @PreAuthorize("@ss.hasPermi('blogs:note:edit')")
    @Log(title = "笔记管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BsNote bsNote) {
        return toAjax(bsNoteService.updateBsNote(bsNote));
    }

    /**
     * 删除笔记管理
     */
    @PreAuthorize("@ss.hasPermi('blogs:note:remove')")
    @Log(title = "笔记管理", businessType = BusinessType.DELETE)
    @Transactional
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(bsNoteService.deleteBsNoteByIds(ids));
    }

    /**
     * 批量修改笔记
     */
    @Log(title = "笔记管理", businessType = BusinessType.UPDATE)
    @PutMapping("/batchEdit")
    public AjaxResult batchEdit(@RequestParam("ids") List<String> ids, Integer value, String type) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("value", value);
        map.put("type", type);
        return toAjax(bsNoteService.batchUpdateBsNote(map));
    }
}
